#include "GeometricTransformationWindow.h"
#include "RenderWidget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>

GeometricTransformationWindow::GeometricTransformationWindow(QWidget *parent)
    : QWidget(parent)
    , render_(new RenderWidget(this))
{
    setupUi();
}

void GeometricTransformationWindow::setupUi() {
    auto *mainLay = new QHBoxLayout(this);

    // 左侧：控制面板
    ctrlGroup_ = new QGroupBox(tr("几何变换"), this);
    ctrlGroup_->setMaximumWidth(220);
    auto *vLay = new QVBoxLayout(ctrlGroup_);

    // 输入矩阵
    vLay->addWidget(new QLabel(tr("A11, A12:"), ctrlGroup_));
    leA11_ = new QLineEdit(ctrlGroup_);
    leA12_ = new QLineEdit(ctrlGroup_);
    vLay->addWidget(leA11_);
    vLay->addWidget(leA12_);

    vLay->addSpacing(10);
    vLay->addWidget(new QLabel(tr("A21, A22:"), ctrlGroup_));
    leA21_ = new QLineEdit(ctrlGroup_);
    leA22_ = new QLineEdit(ctrlGroup_);
    vLay->addWidget(leA21_);
    vLay->addWidget(leA22_);

    vLay->addSpacing(20);
    btnApply_ = new QPushButton(tr("应用变换"), ctrlGroup_);
    btnReset_ = new QPushButton(tr("重置"), ctrlGroup_);
    vLay->addWidget(btnApply_);
    vLay->addWidget(btnReset_);
    vLay->addStretch();
    ctrlGroup_->setLayout(vLay);

    // 右侧：渲染区
    render_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 布局：控制面板 1, 渲染区 3
    mainLay->addWidget(ctrlGroup_, 1);
    mainLay->addWidget(render_, 3);
    setLayout(mainLay);

    // 连接信号槽
    connect(btnApply_, &QPushButton::clicked, this, &GeometricTransformationWindow::onApplyClicked);
    connect(btnReset_, &QPushButton::clicked, this, &GeometricTransformationWindow::onResetClicked);
}

void GeometricTransformationWindow::onApplyClicked() {
    // 读取用户输入的矩阵元素
    float a11 = leA11_->text().toFloat();
    float a12 = leA12_->text().toFloat();
    float a21 = leA21_->text().toFloat();
    float a22 = leA22_->text().toFloat();

    // 将矩阵作为 QVector4D 传给 RenderWidget：(a11,a12,a21,a22)
    render_->showMatrixTransform({a11, a12, a21, a22});
}


void GeometricTransformationWindow::onResetClicked() {
    render_->resetView();
    render_->clearTransform();  // 如果需要同时清除当前变换
}
